use crate::sql::{
    dialects::{
        condition::{Condition, Where, WhereAppend},
        schema::{self, schema::Schema},
    },
    schema::table::TableSchema,
};

use super::builder::ExecuteBuilder;
use sqlx::Database;

#[derive(Debug)]
pub struct DeleteBuilder<'a> {
    table: TableSchema,
    default_schema: &'a str,
    wh: Option<Where>,
}

impl<'a> DeleteBuilder<'a> {
    pub fn new(table: TableSchema) -> Self {
        Self {
            table,
            default_schema: "",
            wh: None,
        }
    }

    pub fn with_default_schema(mut self, schema: &'a str) -> Self {
        self.default_schema = schema;
        self
    }
}
impl<'a> WhereAppend<Condition> for DeleteBuilder<'a> {
    fn and(mut self, cond: Condition) -> Self {
        if let Some(w) = self.wh {
            self.wh = Some(w.and(cond));
        } else {
            self.wh = Some(Where::new(cond));
        }
        self
    }

    fn or(mut self, cond: Condition) -> Self {
        if let Some(w) = self.wh {
            self.wh = Some(w.or(cond));
        } else {
            self.wh = Some(Where::new(cond));
        }
        self
    }
}

impl<'a> WhereAppend<Where> for DeleteBuilder<'a> {
    fn and(mut self, wh: Where) -> Self {
        if let Some(w) = self.wh {
            self.wh = Some(w.and(wh));
        } else {
            self.wh = Some(wh);
        }
        self
    }

    fn or(mut self, wh: Where) -> Self {
        if let Some(w) = self.wh {
            self.wh = Some(w.or(wh));
        } else {
            self.wh = Some(wh);
        }
        self
    }
}

#[cfg(feature = "postgres")]
use sqlx::Postgres;

impl<'a> ExecuteBuilder for DeleteBuilder<'a> {
    #[cfg(feature = "postgres")]
    type DB = Postgres;

    async fn execute<C>(
        &self,
        conn: &mut C,
    ) -> Result<<Self::DB as sqlx::Database>::QueryResult, sqlx::Error>
    where
        for<'e> &'e mut C: sqlx::Executor<'e, Database = Self::DB>,
    {
        let schema = schema::new(self.default_schema.to_string());

        let sql = schema.sql_delete(&self.table, self.wh.clone(), false);

        let mut query: sqlx::query::Query<'_, Self::DB, <Self::DB as Database>::Arguments<'_>> =
            sqlx::query::<Self::DB>(&sql);

        if let Some(w) = &self.wh {
            query = w.bind_to_query(query);
        }

        let result = query.execute(conn).await.map_err(|err| {
            tracing::error!(
                "easy-sqlx: {} [{}]\n{:?}",
                sql,
                if let Some(w) = &self.wh {
                    w.list_params()
                } else {
                    "".to_string()
                },
                err
            );
            err
        })?;
        #[cfg(feature = "logsql")]
        tracing::info!(
            "easy-sqlx: {sql} [{}]\nrows_affected: {}",
            if let Some(w) = &self.wh {
                w.list_params()
            } else {
                "".to_string()
            },
            result.rows_affected()
        );
        Ok(result)
    }

    #[cfg(feature = "postgres")]
    async fn execute_return<'e, 'c: 'e, C, O>(&self, executor: C) -> sqlx::Result<Vec<O>>
    where
        C: 'e + sqlx::Executor<'c, Database = Self::DB>,
        O: 'e,
        for<'r> O: sqlx::FromRow<'r, <Self::DB as Database>::Row>,
        O: std::marker::Send,
        O: Unpin,
    {
        let schema = schema::new(self.default_schema.to_string());

        let sql = schema.sql_delete(&self.table, self.wh.clone(), true);

        let mut query = sqlx::query_as::<Self::DB, O>(&sql);

        if let Some(w) = &self.wh {
            query = w.bind_to_query_as(query);
        }

        let result = query.fetch_all(executor).await.map_err(|err| {
            tracing::error!(
                "easy-sqlx: {} [{}]\n{:?}",
                sql,
                if let Some(w) = &self.wh {
                    w.list_params()
                } else {
                    "".to_string()
                },
                err
            );
            err
        });
        #[cfg(feature = "logsql")]
        tracing::info!(
            "easy-sqlx: {sql} [{}]",
            if let Some(w) = &self.wh {
                w.list_params()
            } else {
                "".to_string()
            }
        );
        result
    }
}
